<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/18 0018
  Time: 14:38
  To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>后台日志</title>
    <jsp:include page="/commons/common-js.jsp"></jsp:include>
</head>
<body>
<table class="layui-hide" id="divtable" lay-filter="divtable"></table>

<script type="text/html" id="divtoolbar">
<%--    <div class="layui-form  layui-inline " lay-filter="divform">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="value" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="get">查询</button>
            </div>
        </div>
    </div>--%>
    <div class="layui-inline">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="back">后退</button>
        </div>
    </div>
    <div class="layui-inline">
        <div style="color: #8D8D8D">
            |
        </div>
    </div>
    <div class="layui-input-inline" >
        <button type="button" class="layui-btn  layui-btn-sm" id="upadd">
            <i class="layui-icon ">&#xe67c;</i>拖动/点击文件
        </button>
    </div>
    <div class="layui-inline">
        <div style="color: #8D8D8D">
            |
        </div>
    </div>
    <div class="layui-form  layui-inline " lay-filter="divform">
        <div class="layui-inline" style=" width: 300px">
            <div class="layui-input-inline" style=" width: 300px">
                <input type="text" name="value" autocomplete="off" value="sh petrel.sh start zlobby" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="run">运行</button>
            </div>
        </div>
    </div>
    <div class="layui-inline">
        <div style="color: #8D8D8D">
            |
        </div>
    </div>
    <div class="layui-form  layui-form-pane layui-inline" lay-filter="upform">
        <div class="layui-inline">
            <label class="layui-form-label">更新地址</label>
            <div class="layui-input-block" style="width: 500px">
                <input type="text" name="url" lay-verify="title" autocomplete="off"
                       value="http://39.100.69.177:7071/down" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">文件名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-btn-container" style="width: 100px;">
            <button class="layui-btn layui-btn-sm" lay-event="filedown">下载到此文件夹</button>
        </div>
    </div>
</script>

<script type="text/html" id="divtool">
    {{# if(!d.div){ }}
    <div class="layui-inline">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-xs" lay-event="down">下载</button>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-xs" lay-event="downfile">链接</button>
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-xs" lay-event="del">删除</button>
        </div>
    </div>
    {{# } }}
</script>

<script type="text/html" id="runtool">
    {{# if(!d.div&&d.name.indexOf('.bak')==-1){ }}
    <div class="layui-inline">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-xs" lay-event="up">更新</button>
            <button class="layui-btn layui-btn-xs" lay-event="rollback">回滚</button>
        </div>
    </div>
    {{# } }}
    {{# if(!d.div&&d.name.indexOf('-SNAPSHOT')>-1&&'r'==d.name.substring(d.name.length-1,d.name.length)){ }}
    <div class="layui-inline">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-xs" lay-event="run">启动</button>
        </div>
    </div>
    {{# } }}

</script>

<script>
    layui.use(['table', 'form', 'laytpl','upload'], function () {
        var table = layui.table,
            form = layui.form,
            upload = layui.upload,
            laytpl = layui.laytpl

        var parent = '';
        var path = '/';
        var tableins = table.render({
            elem: '#divtable'
            , url: '/div/all'
            , defaultToolbar: []
            , toolbar: '#divtoolbar'
            , cols: [[
                {field: 'name', width: 500,templet:isdiv, title: '名字', sort: true}
                ,{field: 'size', width: 100,templet:sizetemplet, title: '大小', sort: true}
                ,{field: 'div', width: 100,templet:divtemplet, title: '类型', sort: true}
                ,{field: 'time', width: 160, title: '时间', sort: true}
                ,{field: 'utils', width: 160,templet:"#divtool", title: '操作'}
                ,{field: 'run', width: 160,templet:"#runtool", title: '操作'}
            ]]
            , page: true
            , limits: [19, 50, 100]
            , limit: 19
            , height: '820'
            , done: function(res, curr, count){
                if (res.data[0]){
                    parent = res.data[0].parent
                    var idx = -1;
                    if ((idx = res.data[0].path.lastIndexOf("/"))>0){
                        path = res.data[0].path.substring(0,idx)
                    }
                }

                //上传按钮
                //获取表单区域所有值
                var loading;
                var uploadInst = upload.render({
                    elem: '#upadd' //绑定元素
                    , url: '/up' //上传接口
                    , number: 1
                    ,accept:'file'
                    , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                        this.data = {
                            'path':path
                        }
                        //loading层
                        loading = layer.load(1, {
                            time:0,
                            shade: [0.4,'#fff'] //0.1透明度的白色背景
                        });
                    }
                    , done: function (res) {
                        layer.close(loading)
                        tableins.reload()
                    }
                    , error: function (index, upload) {
                        //当上传失败时，你可以生成一个“重新上传”的按钮，点击该按钮时，执行 upload() 方法即可实现重新上传
                        layer.close(loading)
                    }
                });

            }
        });

        //头工具栏事件
        table.on('toolbar(divtable)', function(obj){
            switch(obj.event){
                case 'run' :
                    var data = form.val("divform")
                    $.ajax({
                        url:"/div/sh",
                        data:{
                            'sh':data.value
                        },
                        success:function(res){
                            layer.open({
                                type: 1,
                                skin: 'layui-layer-rim', //加上边框
                                area: ['800px', '500px'], //宽高
                                content: res.msg,
                            });
                        }
                    })
                    break;
                case 'back' :

                    tableins.reload({
                        where: {
                            'path':parent
                        }
                    })
                    break;
                case 'filedown' :
                    var url = form.val('upform').url;
                    var name = form.val('upform').name;
                    console.debug(path)
                    $.ajax({
                        url:"/div/up",
                        data:{
                            'url':url,
                            'path':path+'/'+name
                        },
                        success:function(res){
                            tableins.reload()
                            layer.msg(res.msg)
                        }
                    })
                    break;
            };
            form.render()
        });
        //行工具栏事件
        table.on('tool(divtable)', function(obj){

            switch(obj.event){
                case 'opendiv' :
                    var data = obj.data
                    tableins.reload({
                        where: {
                            'path':data.path
                        }
                    })
                    break;
                case 'down' :
                    var data = obj.data
                    var urlFile = "/down"+data.path;
                    var elemIF = document.createElement("iframe");
                    elemIF.src = urlFile;
                    elemIF.style.display = "none";
                    document.body.appendChild(elemIF);
                    break;
                case 'downfile' :

                    var url = location.href;

                    var idx = -1;
                    if (url.indexOf("http://")>-1){

                        idx = url.replace("http://","").indexOf("/")+7;
                    } else if (url.indexOf("https://")>-1) {

                        idx = url.replace("https://","").indexOf("/")+8;
                    }else {
                        idx = url.indexOf("/");
                    }

                    url = url.substring(0,idx)

                    var data = obj.data
                    var urlFile = url+"/down"+data.path;
                    var l = layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['450px', '160px'], //宽高
                        content: `<div style="padding: 20px"><input value="`+urlFile+`" style="width: 400px;" id='tempinput' ></div>`,
                        btn:['复制','关闭'],
                        yes:function () {
                            var input = $('#tempinput');
                            input.select();
                            if (document.execCommand('copy')) {
                                document.execCommand('copy');
                                layer.close(l)
                                layer.msg('复制成功');
                            }
                        }
                    });


                    break;
                case 'del' :
                    var data = obj.data
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['200px', '150px'], //宽高
                        content: `<div style="padding: 20px 0 0 20px">确定删除文件？</div>`,
                        btn:["确定","取消"],
                        yes(index, layero){
                            $.ajax({
                                url: '/del'+data.path,
                                success: function (res) {
                                    layer.msg(res.msg);
                                    tableins.reload()
                                    layer.close(index)
                                }
                            });
                        }
                    });
                    break;
                case 'run' :
                    //loading层
                    var loading = layer.load(1, {
                        time:0,
                        shade: [0.4,'#fff'] //0.1透明度的白色背景
                    });
                    var name = obj.data.name;
                    var idx = name.indexOf('-1.0')
                    var subname = name.substring(0,idx)
                    idx = subname.lastIndexOf('-')
                    subname = subname.substring(idx+1)
                    var start = 'sh start.sh start '+subname;
                    $.ajax({
                        url:"/div/sh",
                        data:{
                            'sh':start
                        },
                        success:function(res){
                            layer.close(loading)
                            layer.open({
                                type: 1,
                                skin: 'layui-layer-rim', //加上边框
                                area: ['900px', '500px'], //宽高
                                content: res.msg,
                            });
                        }
                    })
                    break;
                case 'up' :
                    var loading = layer.load(1, {
                        time:0,
                        shade: [0.4,'#fff'] //0.1透明度的白色背景
                    });
                    var data = obj.data
                    var url = form.val('upform').url;
                    $.ajax({
                        url:"/div/up",
                        data:{
                            'url':url,
                            'path':data.path
                        },
                        success:function(res){
                            layer.close(loading)
                            tableins.reload()
                            layer.msg(res.msg)
                        }
                    })
                    break;
                case 'rollback' :
                    var loading = layer.load(1, {
                        time:0,
                        shade: [0.4,'#fff'] //0.1透明度的白色背景
                    });
                    var data = obj.data;
                    $.ajax({
                        url:"/div/rollback",
                        data:{
                            'path':data.path
                        },
                        success:function(res){
                            layer.close(loading)
                            tableins.reload()
                            layer.msg(res.msg)
                        }
                    })
                    break;
            };
            upload.render();
        });


        function isdiv(d) {
            if (d.div){
                return `<div class="open-div" style="color: coral" lay-event="opendiv">`+d.name+`</div>`;
            }
            return `<div>`+d.name+`</div>`;
        }

        function divtemplet(d) {
            if (d.div){
                return '文件夹';
            }
            var idx = d.name.lastIndexOf(".")
            if (idx>0){
                return d.name.substring(idx, d.name.length);
            }
        }
        function sizetemplet(d) {
            if (d.size!=null&&d.size>-1){
                return d.size+"M";
            }

            return "";
        }

        $("div.open-div").on('click',function () {
            console.debug(this)
        })

    });

</script>

</body>
</html>
